package pers.cnzdy.ipcclient;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.List;

import pers.cnzdy.ipcserver.IQuiz;
import pers.cnzdy.ipcserver.Quiz;

public class ClientActivity extends AppCompatActivity {

    private final String TAG = "Client";

    private IQuiz quizOperator;
    private boolean connected;
    private List<Quiz> quizList;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            quizOperator = IQuiz.Stub.asInterface(service);
            connected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            connected = false;
        }
    };

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_getQuizList:
                    if (connected) {
                        try {
                            quizList = quizOperator.getQuizList();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        log();
                    }
                    break;
                case R.id.btn_addQuiz:
                    if (connected) {
                        Quiz quiz = new Quiz("简述RTS/CTS握手协议。");
                        try {
                            quizOperator.addQuiz(quiz);
                            Log.i(TAG, "向服务器以InOut方式新增一道题目。");
                            Log.i(TAG, "题目：" + quiz.getStatement());
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_getQuizList).setOnClickListener(clickListener);
        findViewById(R.id.btn_addQuiz).setOnClickListener(clickListener);
        startService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connected) {
            unbindService(serviceConnection);
        }
    }

    private void startService() {
        Intent intent = new Intent();
        intent.setPackage("pers.cnzdy.ipcserver");
        intent.setAction("pers.cnzdy.ipcserver.action");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void log() {
        for (Quiz quiz : quizList) {
            Log.e(TAG, quiz.toString());
        }
    }
}
